package com.industrial.im.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.domain.R;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.im.domain.DispatchKeyItemManagement;
import com.industrial.im.domain.vo.NameAndValueVo;
import com.industrial.im.mapper.DispatchKeyItemManagementMapper;
import com.industrial.im.service.IDispatchStatisticsService;
import com.industrial.system.api.RemoteDeptService;
import com.industrial.system.api.RemoteUserService;
import com.industrial.system.api.domain.SysDept;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *     统计页面Controller
 * </p>
 * @author sangguangcan
 */
@Service
public class IDispatchStatisticsServiceImpl implements IDispatchStatisticsService {

    @Autowired
    private DispatchKeyItemManagementMapper dispatchKeyItemManagementMapper;

    @Autowired
    private RemoteDeptService remoteDeptService;

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询状态的数量
     *
     * @return
     */
    @Override
    public List<Map<String,Object>> queryStatusCount() {
        DispatchKeyItemManagement params = new DispatchKeyItemManagement();
        List<DispatchKeyItemManagement> dispatchKeyItemManagements = dispatchKeyItemManagementMapper.selectDispatchKeyItemManagementStatsList(params);
        if(dispatchKeyItemManagements != null && !dispatchKeyItemManagements.isEmpty()){
            Map<String, Long> statusCount = dispatchKeyItemManagements.stream().filter(k -> StringUtils.isNotBlank(k.getItemStatus())).collect(Collectors.groupingBy(DispatchKeyItemManagement::getItemStatus, Collectors.counting()));
            List<Map<String,Object>> list = new ArrayList<>();
            for(Map.Entry<String, Long> r : statusCount.entrySet()){
                String key = r.getKey();
                Long value = r.getValue();
                Map<String,Object> tempMap = new HashMap<>();
                tempMap.put("name",key);
                tempMap.put("value",value);
                list.add(tempMap);
            }
            return list;
        }
        return new ArrayList<Map<String,Object>>();
    }

    /**
     * 重点事项类型占比
     *
     * @return
     */
    @Override
    public List<Map> queryItemTypeCount() {
        List<Map> map = dispatchKeyItemManagementMapper.queryItemTypeCount();
        return map;
    }

    /**
     *
     */
    @Override
    public Map<String,Object> sjddyj() {

        //查询任务总数
        List<DispatchKeyItemManagement> dispatchKeyItemManagements = dispatchKeyItemManagementMapper.selectDispatchKeyItemManagementList(new DispatchKeyItemManagement());
        if(dispatchKeyItemManagements != null && !dispatchKeyItemManagements.isEmpty()){

            Map<String,Object> resultMap = new HashMap<>();

            //查询已完成
            Map<String,Long> sjddyjYwc = dispatchKeyItemManagementMapper.sjddyjYwc();
            //查询未完成
            Map<String,Long> sjddyjWwc = dispatchKeyItemManagementMapper.sjddyjWwc();
            int size = dispatchKeyItemManagements.size();
            BigDecimal totalTaskCount = BigDecimal.valueOf(size);
            if(sjddyjWwc.get("ywc") != null){
                Long ywc = sjddyjYwc.get("ywc");
                BigDecimal ywcBigDecimal = BigDecimal.valueOf(ywc);
                BigDecimal ywcL = ywcBigDecimal.divide(totalTaskCount, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                resultMap.put("ywc",ywcL);
            }else{
                resultMap.put("ywc","0");
            }

            if(sjddyjWwc.get("wwc") != null){
                Long wwc = sjddyjWwc.get("wwc");
                BigDecimal wwcBigDecimal = BigDecimal.valueOf(wwc);
                BigDecimal wwcL = wwcBigDecimal.divide(totalTaskCount, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                resultMap.put("wwc",wwcL);
            }else{
                resultMap.put("wwc","0");
            }
            return resultMap;
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("ywc","0");
        resultMap.put("wwc","0");
        return resultMap;
    }

    /**
     * <p>
     * 查询任务的数量
     * </p>
     *
     * @return
     */
    @Override
    public Map<String, Long> queryEventItemCount() {
        DispatchKeyItemManagement params = new DispatchKeyItemManagement();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles = loginUser.getRoles();
        SysUser sysUser = loginUser.getSysUser();
        if(sysUser.isAdmin()){
            params.setProvince("370000");
        }else{
            if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
                params.setCounty(sysUser.getDistrict());
            }else if(ArrayUtil.contains(roles.toArray(),"s_01_011")){
                params.setCity(sysUser.getCity());
            }else if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
                params.setProvince(sysUser.getProvince());
            }
        }

        List<DispatchKeyItemManagement> dispatchKeyItemManagements = dispatchKeyItemManagementMapper.selectDispatchKeyItemManagementStatsList(params);
        if(dispatchKeyItemManagements != null && !dispatchKeyItemManagements.isEmpty()){
            Map<String, Long> statusCount = dispatchKeyItemManagements.stream().filter(k -> StringUtils.isNotBlank(k.getItemStatus())).collect(Collectors.groupingBy(DispatchKeyItemManagement::getItemStatus, Collectors.counting()));

            if(!statusCount.containsKey("待签收")){
                statusCount.put("待签收",0L);
            }

            if(!statusCount.containsKey("办理中")){
                statusCount.put("办理中",0L);
            }

            if(!statusCount.containsKey("待验收")){
                statusCount.put("待验收",0L);
            }

            if(!statusCount.containsKey("已验收")){
                statusCount.put("已验收",0L);
            }

            Long count = 0L;
            for(Map.Entry<String, Long> t : statusCount.entrySet()){
                Long value = t.getValue();
                count = value + count;
            }
            statusCount.put("任务总数",count);
            return statusCount;
        }
        return null;
    }

    /**
     * <p>
     * 查询部门的事项数量
     * </p>
     *
     * @return
     */
    @Override
    public List<NameAndValueVo> queryDeptEventItemCount() {
        List<NameAndValueVo> nameAndValueVos = dispatchKeyItemManagementMapper.queryDeptEventItemCount();
        if(nameAndValueVos != null && !nameAndValueVos.isEmpty()){

            //统计到所有的负责人
            List<String> itemHeaderIdList = nameAndValueVos.stream().map(NameAndValueVo::getName).collect(Collectors.toList());

            //查询负责人所在的部门及名称
            if(itemHeaderIdList != null && !itemHeaderIdList.isEmpty()){
                List<String> collect = itemHeaderIdList.stream().distinct().collect(Collectors.toList());
                R<Map<String, String>> mapR = remoteUserService.queryDeptIdAndDeptNameByUserId(collect);
                if(mapR.getCode() == R.SUCCESS){
                    Map<String, String> data = mapR.getData();
                    for(NameAndValueVo p : nameAndValueVos){
                        String name = p.getName();
                        String s = data.get(name);
                        p.setName(s);
                    }
                }
            }
//            List<String> itemHeaderIdList = nameAndValueVos.stream().map(NameAndValueVo::getName).collect(Collectors.toList());
//            if(itemHeaderIdList != null && !itemHeaderIdList.isEmpty()){
//                R<List<SysDept>> listR = remoteDeptService.queryDeptNameByDeptId(itemHeaderIdList);
//                if(listR.getCode() == R.SUCCESS){
//                    List<SysDept> data = listR.getData();
//                    if(data != null && !data.isEmpty()){
//                        Map<String, String> deptNameMap = data.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
//                        for(NameAndValueVo t : nameAndValueVos){
//                            String name = t.getName();
//                            String s = deptNameMap.get(name);
//                            t.setName(s);
//                        }
//                    }
//                }
//            }
        }
        return nameAndValueVos;
    }

    /**
     * <p>
     * 查询到期数量
     * </p>
     *
     * @return
     */
    @Override
    public Map<String, List<String>> queryDqCount() {
        List<NameAndValueVo> nameAndValueVos = dispatchKeyItemManagementMapper.queryDqCount();
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        for(NameAndValueVo temp : nameAndValueVos){
            xAxis.add(temp.getName());
            yAxis.add(temp.getValue());
        }
        Map<String, List<String>> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }
}
